#include "func_iic_slave.h"
#include "IICSlave.h"
#include "usb_com.h"
#include "string.h"
#include "mem_malloc.h"
static unsigned short iicSlaveTimeout = 1000;
static unsigned char readLen;
static unsigned char *iicReadBuf = 0;
static unsigned char *iicWriteBuf = 0;
void iic_slave_set_param(unsigned char cmd, unsigned char subCmd, unsigned char *data)
{
    iic_slave_disable();
    iic_set_param(data[5]);
    usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_SET_PARAM, (void *)COM_OK);
}
void iic_slave_write_data(unsigned char cmd, unsigned char subCmd, unsigned char *data)
{
    m_malloc(iicWriteBuf, data[2] - 6);
    if (iicWriteBuf == 0)
    {
        usb_cmd(COMM_IIC_SLAVE, COMM_SUB_SPI_SLAVE_WRITE, (void *)COM_MEM);
    }
    else
    {
        iicSlaveTimeout = (data[5]<<8)+data[6];
        memcpy(iicWriteBuf, &data[7], data[2] - 8);
        if (iic_send(iicWriteBuf, data[2] - 8, iicSlaveTimeout) != IIC_FAIL)
        {
            //usb_cmd(COMM_IIC_SLAVE, COMM_SUB_SPI_SLAVE_WRITE, (void *)COM_OK);
            return;
        }
        else
        {
            m_free(iicWriteBuf);
            usb_cmd(COMM_IIC_SLAVE, COMM_SUB_SPI_SLAVE_WRITE, (void *)COM_FAIL);
        }
    }
}

void iic_slave_read_data(unsigned char cmd, unsigned char subCmd, unsigned char *data)
{
    readLen = data[5];
    m_malloc(iicReadBuf, BUFF_SIZE);
    if (iicReadBuf == 0)
    {
        usb_cmd(COMM_SPI_SLAVE, COMM_SUB_IIC_SLAVE_READ, (void *)COM_MEM);
    }
    else
    {
        iicSlaveTimeout = (data[6]<<8)+data[7];
        if (iic_rec(iicReadBuf, data[5], iicSlaveTimeout) != IIC_FAIL)
        {
            //usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_READ, (void *)COM_OK);
            return;
        }
        else
        {
            m_free(iicReadBuf);
            usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_READ, (void *)COM_FAIL);
        }
    }

}
void iic_slave_read_update(unsigned char cmd, unsigned char subCmd, void *param)
{
    unsigned char sendData[USB_PORT_SIZE] = {0};
    unsigned char len = readLen;
    sendData[0] = 0xaa;
    sendData[1] = 0x55;
    sendData[2] = len + 7;
    sendData[3] = COMM_IIC_SLAVE;
    sendData[4] = COMM_SUB_IIC_SLAVE_READ;
    sendData[5] = (unsigned char)param;
    if (iicReadBuf != 0)
    {
        memcpy(&sendData[6], iicReadBuf, len);
        m_free(iicReadBuf);
    }
    sendData[len + 6] = chksum(sendData, len + 6);
    usb_send(sendData, len + 7);
}

void iic_tx_cplt()
{
	usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_READ, (void *)COM_OK);
    m_free(iicWriteBuf);
    iicWriteBuf = 0;
}
void iic_rx_cplt()
{
    usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_READ, (void *)COM_OK);
}
void iic_comm_stop(unsigned char state)
{
	if (state == iic_slave_rec)
    {
        usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_READ, (void *)COM_STOP);
    }
    else
    {
        m_free(iicWriteBuf);
        usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_WRITE, (void *)COM_STOP);
    }
}
void iic_comm_timeout(unsigned char state)
{
    if (state == iic_slave_rec)
    {
        usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_READ, (void *)COM_TIMEOUT);
    }
    else if (state == iic_slave_send)
    {
        m_free(iicWriteBuf);
        usb_cmd(COMM_IIC_SLAVE, COMM_SUB_IIC_SLAVE_WRITE, (void *)COM_TIMEOUT);
    }
}
